package com.Xht.www.servlet;

//后端响应更新账单流水的请求

import com.Xht.www.dao.WalletTransactionLogDAO;
import com.Xht.www.po.WalletTransactionLogPO;
import com.google.gson.Gson;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/InsertTransactionLogServlet")
public class InsertTransactionLogServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int userId = Integer.parseInt(request.getParameter("userId"));
        int walletId = Integer.parseInt(request.getParameter("walletId"));
        String transactionType = request.getParameter("transactionType");
        BigDecimal transactionAmount = new BigDecimal(request.getParameter("transactionAmount"));
        Timestamp transactionTime = new Timestamp(System.currentTimeMillis());

        WalletTransactionLogPO log = new WalletTransactionLogPO();
        log.setUserId(userId);
        log.setWalletId(walletId);
        log.setTransactionType(transactionType);
        log.setTransactionAmount(transactionAmount);
        log.setTransactionTime(transactionTime);

        WalletTransactionLogDAO logDao = new WalletTransactionLogDAO();
        boolean success = logDao.insert(log);

        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        Map<String, Boolean> result = new HashMap<>();
        result.put("success", success);
        Gson gson = new Gson();
        out.print(gson.toJson(result));
        out.flush();
    }
}